<template>
	<view class="editbook">
		<div class="edit-header">
			<div class="reject" @click="editFalse()">
				<image src="../../static/reject.png" mode=""></image>
			</div>
			<text>已选择一本书籍</text>
			<div class="confim" @click="editTrue()">
				<image src="../../static/confim.png" mode=""></image>
			</div>
		</div>
		<div class="edit-none" v-if="isOpen">
			<image src="../../static/noneBook.png" mode=""></image>
			<text>暂无书籍,请前往书城加入</text>
		</div>
		<div class="edit-banner" v-else>
			<ul>
				<li v-for="(item,i) in bookselfList" @click="addPoint(item)">
					<div class="banner-list">
						<div class="banner-img">
							<image :src="item.book_img" mode=""></image>
							<div class="box" v-if="item.isShow">
								<image src="../../static/confim-active.png" mode=""></image>
							</div>
						</div>
						<div class="banner-con">
							<text>{{item.book_title}}</text>
							<p>{{item.book_author}}</p>
						</div>
					</div>
				</li>
			</ul>
		</div>
		<div class="edit-footer">
			<ul>
				<li @click="addAll()">
					<div class="footer-list">
						<image src="../../static/allEdit.png" mode=""></image>
						<text>全选</text>
					</div>
				</li>
				<li>
					<div class="footer-list" @click="deleteBook()">
						<image src="../../static/delete-active.png" mode=""></image>
						<text>删除</text>
					</div>
				</li>
				<li @click="changeSort()">
					<div class="footer-list">
						<image src="../../static/listOrder.png" mode=""></image>
						<text>按時間排序</text>
					</div>
				</li>
			</ul>
		</div>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bookselfList: [],
				isShow: false,
				isSort: false,
				isAll: false,
				isOpen: false
			}
		},
		onLoad() {
			//调用云函数，查询书架
			uniCloud.callFunction({
					name: 'list_bookself',
					data: {}
				})
				.then(content => {
					// console.log('1.0',content);
					let {
						result: {
							data
						}
					} = content;
					if(data.length==0){
						this.isOpen = true
					}else{
						this.isOpen = false
					}
					for (let k = 0; k < data.length; k++) {
						data[k].isShow = this.isShow
					}
					this.bookselfList = data;
				})


		},
		methods: {
			addAll() {
				this.isShow = !this.isShow;
				this.isAll = !this.isAll
				for (let k = 0; k < this.bookselfList.length; k++) {
					this.bookselfList[k].isShow = this.isShow;
				}
			},
			addPoint(item) {
				// console.log(item);
				if (!item.isShow) {
					item.isShow = true;
				} else {
					item.isShow = false;
				}
			},
			deleteBook() {
				if(this.isAll){
					let that = this;
					uni.showModal({
						title: '您确定删除全部书籍吗?',
						success(res) {
							if(res.confirm){
								uniCloud.callFunction({
									name:'delete_allBook',
									data:{}
								}).then(content=>{
									console.log('1.0',content);
									uni.showToast({
										title:'删除全部成功',
										icon:'success'
									})
									that.bookselfList = []
									this.isOpen = true
								})
							}else if(res.cancel){
								uni.showToast({
									title:'取消删除全部书籍',
									icon:'none'
								})
								
							}
						}
					})
				}else{
					for (let k = 0; k < this.bookselfList.length; k++) {
						if(this.bookselfList[k].isShow){
							console.log(this.bookselfList[k].isShow);
							
							let that = this;
							uni.showModal({
								title: '您确定删除该书籍吗?',
								success(res) {
									if(res.confirm){
										uniCloud.callFunction({
											name:'delete_bookself',
											data:{book_id: that.bookselfList[k].book_id}
										}).then(content=>{
											console.log('1.0',content);
											uni.showToast({
												title:'删除书籍成功',
												icon:'success'
											})
											let arr = that.bookselfList.filter(item=>{
												if(item.book_id != that.bookselfList[k].book_id){
													return item;
												}
											})
											that.bookselfList = arr;
											if(that.bookselfList.length==0){
												this.isOpen = true
											}else{
												this.isOpen = false
											}
										})
									}else if(res.cancel){
										uni.showToast({
											title:'取消删除书籍',
											icon:'none'
										})
										that.bookselfList[k].isShow = false
									}
								}
							})
						}
					}
				
				}
				
			},
			editFalse(){
				uni.showToast({
					title:'取消编辑',
					icon: 'none'
				})
				uni.switchTab({
					url:'../bookshelf/bookshelf'
				})
			},
			editTrue(){
				uni.showToast({
					title:'保存编辑',
					icon: 'success'
				})
				uni.switchTab({
					url:'../bookshelf/bookshelf'
				})
			},
			
			// 排序
			sortTimeTop(array, key) {
				return array.sort((b, a) => {
					let x = a[key];
					let y = b[key];
					return y - x
				})
			},
			sortTimeUnder(array, key) {
				return array.sort((b, a) => {
					let x = a[key];
					let y = b[key];
					return x - y
				})
			},
			// 改变排序状态值
			changeSort() {
				this.isSort = !this.isSort
				if (this.isSort) {
					let top = this.sortTimeTop(this.bookselfList, "book_create_time")
					this.bookselfList = top
				} else {
					let under = this.sortTimeUnder(this.bookselfList, "book_create_time")
					this.bookselfList = under
				}
			}
		}
	}
</script>

<style lang="scss">
	.editbook {
		position: relative;
		background-image: url("../../static/background-image.png");
		// left: -10px;
		top: 0px;
		width: 100%;
		height: 2000rpx;
		opacity: 1;
		overflow: hidden;
		.edit-none{
			padding-top: 100rpx;
			width: 100%;
			height: 950rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			background-color: #fceccc;
			image{
				width: 300rpx;
				height: 300rpx;
			}
			text{
				color: rgba(119, 130, 138, 1);
				margin-top: 40rpx;
				font-weight: 600;
				font-size: 18px;
			}
		}
		.edit-header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 88%;
			margin: 40rpx auto;

			.reject {
				width: 50rpx;
				height: 50rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			text {
				font-weight: 600;
				color: #3B3B3B;
				font-size: 18px;
			}

			.confim {
				width: 50rpx;
				height: 50rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.edit-banner {
			width: 100%;
			height: 1400rpx;
			background-color: #fceccc;

			ul {
				width: 88%;
				margin: 0 auto;
				list-style: none;
				padding: 0;
				display: flex;
				flex-wrap: wrap;
				// justify-content: space-between;
				align-items: center;

				li {
					width: 200rpx;
					height: 270rpx;
					// margin-left: 10rpx;
					box-sizing: border-box;
					margin: 30rpx 10rpx 100rpx 10rpx;

					.banner-list {
						width: 100%;
						height: 100%;

						.banner-img {
							position: relative;
							width: 100%;
							height: 100%;

							// overflow: hidden;
							image {
								width: 100%;
								height: 100%;
								border: 1px solid #fd6537;
								border-radius: 12px;
								overflow: hidden;
							}

							.box {
								position: absolute;
								bottom: 2px;
								right: 2px;
								width: 38rpx;
								height: 38rpx;

								image {
									width: 100%;
									height: 100%;
								}
							}
						}

						.banner-con {
							margin-top: 10rpx;

							text {
								display: block;
								text-overflow: ellipsis;
								overflow: hidden;
								word-break: break-all;
								white-space: nowrap;
								font-size: 16px;
								font-weight: 600;
								color: rgba(59, 59, 59, 1);
							}

							p {
								margin-top: 4rpx;
								text-overflow: ellipsis;
								overflow: hidden;
								word-break: break-all;
								white-space: nowrap;
								color: #A8A8A8;
								font-size: 13px;
							}
						}
					}
				}
			}
		}

		.edit-footer {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 110rpx;
			background-color: #f0cead;

			ul {
				width: 100%;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: space-evenly;
				list-style: none;
				padding: 0;

				li {
					width: 120rpx;
					height: 60rpx;
					margin-right: 50rpx;
					margin-bottom: 12rpx;

					.footer-list {
						width: 160rpx;
						// height: 80rpx;
						display: flex;
						flex-direction: column;
						align-items: center;
						justify-content: space-between;

						image {
							margin-bottom: 8rpx;
							width: 44rpx;
							height: 44rpx;
						}

						text {
							// margin-top: 10rpx;
							font-size: 12.5px;
							color: #4E4E4E;
						}
					}
				}
			}
		}
	}
</style>